package com.zjhd.sealcutting.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.zjhd.sealcutting.domain.UserInfo;
import com.zjhd.sealcutting.param.UserSearchQuery;
import com.zjhd.sealcutting.utils.OperLog;
import com.zjhd.sealcutting.vo.SearchResult;
import com.zjhd.sealcutting.service.UserInfoService;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * TODO  用户管理
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/23 14:17
 */
@Api(value = "用户管理", tags = "用户管理")
@RequestMapping("/user")
@RestController
public class UserInfoController {

    private final static Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "分页查询用户列表接口", notes = "分页查询用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "姓名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", defaultValue = "10", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", defaultValue = "1", dataType = "String", paramType = "query")
    })
    @GetMapping("/queryUserParamlist")
    public ResponseResult<SearchResult<UserInfo>> queryUserParamlist(UserSearchQuery param){
        try{
            return ResultGenerator.genSuccessResult(userInfoService.queryUserParamlist(param));
        }catch (Exception ex) {
            logger.error("分页查询用户列表接口失败", ex);
            return ResultGenerator.genFailResult("分页查询用户列表接口失败");
        }
    }

    @ApiOperation(value = "新增用户", notes = "新增用户接口")
    @PostMapping("/insertUser")
    @OperLog(operModul = "用户管理-新增用户",operType = "新增",operDesc = "新增用户")
    public ResponseResult insertUser(@RequestBody UserInfo userInfo){
        try{
            return ResultGenerator.genSuccessResult(userInfoService.insertUser(userInfo));
        }catch (Exception ex) {
            logger.error("新增用户接口失败", ex);
            return ResultGenerator.genFailResult("新增用户接口失败");
        }
    }

    @ApiOperation(value = "修改用户", notes = "修改用户接口")
    @PostMapping("/updateUser")
    @OperLog(operModul = "用户管理-修改用户",operType = "修改",operDesc = "修改用户")
    public ResponseResult updateUser(@RequestBody UserInfo userInfo){
        try{
            return ResultGenerator.genSuccessResult(userInfoService.updateUser(userInfo));
        }catch (Exception ex) {
            logger.error("修改用户接口失败", ex);
            return ResultGenerator.genFailResult("修改用户接口失败");
        }
    }

    @ApiOperation(value = "查询用户详情", notes = "查询用户详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Long", paramType = "query")
    })
    @GetMapping("/queryUserInfo")
    @OperLog(operModul = "用户管理-查看用户",operType = "查询",operDesc = "查看用户详情")
    public ResponseResult queryUserInfo(Integer id){
        try{
            return ResultGenerator.genSuccessResult(userInfoService.queryUserInfo(id));
        }catch (Exception ex) {
            logger.error("查询用户详情接口失败", ex);
            return ResultGenerator.genFailResult("查询用户详情接口失败");
        }
    }

    @ApiOperation(value = "禁用或启用用户", notes = "禁用或启用用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "启用类型  0:启用 1:禁用", dataType = "Long", paramType = "query")
    })
    @PostMapping("/disableOrEnableUser")
    @OperLog(operModul = "用户管理-禁用或启用用户",operType = "修改",operDesc = "禁用或启用用户")
    public ResponseResult disableOrEnableUser(@RequestParam(value = "id",defaultValue = "")Integer id,@RequestParam(value = "type",defaultValue = "")Integer type){
        try{
            return ResultGenerator.genSuccessResult(userInfoService.disableOrEnableUser(id,type));
        }catch (Exception ex) {
            logger.error("禁用或启用用户接口失败", ex);
            return ResultGenerator.genFailResult("禁用或启用用户接口失败");
        }
    }

    @ApiOperation(value = "获取当前登录用户详情", notes = "获取当前用户登录详情接口")
    @GetMapping("/queryCurrentUserInfo")
    public ResponseResult queryCurrentUserInfo(HttpServletRequest request, HttpServletResponse response){
        try{
            return ResultGenerator.genSuccessResult(userInfoService.queryCurrentUserInfo(request,response));
        }catch (Exception ex) {
            logger.error("获取当前登录用户详情接口失败", ex);
            return ResultGenerator.genFailResult("获取当前登录用户详情接口失败");
        }
    }

    @ApiOperation(value = "用户图像修改", notes = "用户图像修改接口")
    @PostMapping("/updateByIdPortrait")
    @OperLog(operModul = "用户管理-用户图像修改",operType = "修改",operDesc = "用户图像修改")
    public ResponseResult updateByIdPortrait(@RequestBody UserInfo userInfo)throws Exception {
        try{
            if(userInfo==null){
                return ResultGenerator.genFailResult("请求参数不能为空");
            }
            return userInfoService.updateByIdPortrait(userInfo);
        }catch (Exception ex) {
            logger.error("用户图像修改接口失败", ex);
            return ResultGenerator.genFailResult("用户图像修改失败");
        }
    }

    @ApiOperation(value = "用户信息修改", notes = "用户信息修改接口")
    @PostMapping("/updateByIdUserInfo")
    @OperLog(operModul = "用户管理-用户信息修改",operType = "修改",operDesc = "用户信息修改")
    public ResponseResult updateByIdUserInfo(@RequestBody UserInfo userInfo)throws Exception {
        try{
            if(userInfo==null){
                return ResultGenerator.genFailResult("请求参数不能为空");
            }
            return userInfoService.updateByIdUserInfo(userInfo);
        }catch (Exception ex) {
            logger.error("用户信息修改接口失败", ex);
            return ResultGenerator.genFailResult("用户信息修改失败");
        }
    }

    @ApiOperation(value = "根据手机号和验证码判断验证码是否正确", notes = "根据手机号和验证码判断验证码是否正确接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query")
    })
    @GetMapping("/isVerificationCode")
    public ResponseResult<Boolean> isVerificationCode(String mobile,String code){
        try{
            return ResultGenerator.genSuccessResult(userInfoService.isVerificationCode(mobile,code));
        }catch (Exception ex) {
            logger.error("根据手机号和验证码判断验证码是否正确接口失败", ex);
            return ResultGenerator.genFailResult("根据手机号和验证码判断验证码是否正确接口失败");
        }
    }

    @ApiOperation(value = "判断手机号是否存在", notes = "判断手机号是否存在接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", paramType = "query")
    })
    @GetMapping("/isMobile")
    public ResponseResult<Boolean> isMobile(String mobile) throws Exception{
        try{
            return ResultGenerator.genSuccessResult(userInfoService.isMobile(mobile));
        }catch (Exception ex) {
            logger.error("判断手机号是否存在接口失败", ex);
            return ResultGenerator.genFailResult("判断手机号是否存在接口失败");
        }
    }

    @ApiOperation(value = "查询企业集合", notes = "查询企业集合")
    @GetMapping("/queryGroupByCompany")
    public ResponseResult<List<String>> queryGroupByCompany() throws Exception{
        try{
            return ResultGenerator.genSuccessResult(userInfoService.queryGroupByCompany());
        }catch (Exception ex) {
            logger.error("查询企业集合接口失败", ex);
            return ResultGenerator.genFailResult("查询企业集合接口失败");
        }
    }

    @ApiOperation(value = "根据企业查询用户", notes = "根据企业查询用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "company", value = "企业名称", dataType = "String", paramType = "query")
    })
    @GetMapping("/queryByCompanyUser")
    public ResponseResult<List<UserInfo>> queryByCompanyUser(String company) throws Exception{
        try{
            return ResultGenerator.genSuccessResult(userInfoService.queryByCompanyUser(company));
        }catch (Exception ex) {
            logger.error("根据企业查询用户接口失败", ex);
            return ResultGenerator.genFailResult("根据企业查询用户接口失败");
        }
    }

    @ApiOperation(value = "查询所有启用用户", notes = "查询所有启用用户接口")
    @GetMapping("/queryAllUser")
    public ResponseResult<List<UserInfo>> queryAllUser() throws Exception{
        try{
            return ResultGenerator.genSuccessResult(userInfoService.queryAllUser());
        }catch (Exception ex) {
            logger.error("查询所有启用用户接口失败", ex);
            return ResultGenerator.genFailResult("查询所有启用用户接口失败");
        }
    }
}
